﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Data;
using DiemChapNhanThe.Entities;

public partial class Tim_Kiem : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Lay Parameter roi goi ham xu ly
            string key = Request.Params["Key"];
            string idDp = Request.Params["IdDiaPhuong"];
            string idLh = Request.Params["IdLoaiHang"];

            if (key == "0") key = "";
            if (key == null || idDp == null || idLh == null)
            {
                lbKoKQ.Visible = true;
                lbKoKQ.Text = "Không tìm thấy kết quả tương ứng";
            }
            else
            {
                Bind_Data(key, Int32.Parse(idDp), Int32.Parse(idLh), Pager1.CurrentIndex);    
            }            
        }
    }

    private void Bind_Data(string key, int idDiaPhuong, int idLoaiHang, int PageNo)
    {
        int TotalRows = 0;
        string Whereclause = "";
        // Kiem tra truong hop
        if ((idDiaPhuong != 0) && (idLoaiHang != 0))
            Whereclause += " dbo.fuChuyenCoDauThanhKhongDau(TieuDe) like N'%" + Common.StandardString(key) + "%' and IdLoaiMatHang=" + idLoaiHang + " and IdDiaPhuong=" + idDiaPhuong;
        else
            if ((idDiaPhuong != 0) || (idLoaiHang != 0))
                Whereclause += " dbo.fuChuyenCoDauThanhKhongDau(TieuDe) like N'%" + Common.StandardString(key) + "%' and (IdLoaiMatHang=" + idLoaiHang + " or IdDiaPhuong=" + idDiaPhuong + ")";
            else
                Whereclause += " (dbo.fuChuyenCoDauThanhKhongDau(TieuDe) like N'%" + Common.StandardString(key) + "%' or IdLoaiMatHang=" + idLoaiHang + " or IdDiaPhuong=" + idDiaPhuong + ")";
        

        DataSet dsMathang = DataRepository.Provider.ExecuteDataSet("MatHang_GetPaged", Whereclause, "NgayCapNhat DESC",
            PageNo - 1, Pager1.PageSize);
        // TList<MatHang> tlistMatHangs = DataRepository.MatHangProvider.GetPaged(Whereclause, "NgayCapNhat DESC", 0, 10,
        //     out TotalRows);
        dlKetQuaTimKiem.DataSource = dsMathang.Tables[0];
        dlKetQuaTimKiem.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsMathang.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {

        }
        Pager1.ItemCount = TotalRows;
        Pager1.Visible = TotalRows > Pager1.PageSize;        
    }

    public string GetTieuDe(object Char)
    {
        var s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        var currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndex;
        Bind_Data(Request.Params["Key"], Int32.Parse(Request.Params["IdDiaPhuong"]), 
            Int32.Parse(Request.Params["IdLoaiHang"]), currnetPageIndex);        
    }
}